package com.baidumap.administrator.happystudy.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.ab.dialog.loadingDialog.LoadingDialogUtil;
import com.ab.http.OnMessageResponse;
import com.ab.util.AbStrUtil;
import com.ab.util.ToastUtil;
import com.ab.view.clear_edit_text.ClearEditText;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.api.Api;
import com.baidumap.administrator.happystudy.base.AppBaseActivity;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.model.Login;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 描述：
 * 作者：xiaoxing on 17/4/19 20:43
 * 邮箱：2235445233@qq.com
 */
public class TuiKuanShouHou extends AppBaseActivity implements OnMessageResponse {

    @Bind(R.id.ced_title)
    ClearEditText cedTitle;
    @Bind(R.id.cet_miaoshu)
    ClearEditText cetMiaoshu;

    private String mCedTitle, mCetMiaoshu;
    private String mOrderNo, mProductId, mShopId;

    @Override
    public int bindLayout() {
        return R.layout.activity_tui_kuan_shou_hou;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        getBundleValue();
    }


    @OnClick(R.id.btn_ltijiao)
    public void onClick() {

        if (checkTitleMiaoshu()) {

            LoadingDialogUtil.showGifdialog(getSupportFragmentManager());
            Api.orderFeedbackSubmit(this, sHelper.getString(Constant.TOKEN), sHelper.getString(Constant.UID), mProductId, mShopId, mOrderNo, mCedTitle, mCetMiaoshu);
        }
    }

    @Override
    public void doBusiness(Context mContext) {
        super.doBusiness(mContext);
        setHeaderBack();
        setHeaderTitle(R.string.tui_kuan_shou_hou);
    }

    private void getBundleValue() {
        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            mProductId = bundle.getString("product_id");
            mOrderNo = bundle.getString("order_id");
            mShopId = bundle.getString("shop_id");

        }
    }

    /**
     * 检查用户名和密码是否为空
     */
    private boolean checkTitleMiaoshu() {

        mCedTitle = getEidtTextValue(cedTitle);
        mCetMiaoshu = getEidtTextValue(cetMiaoshu);

        if (AbStrUtil.isEmpty(mCedTitle)) {
            ToastUtil.showMessage(this, "主要问题不能为空");
            return false;
        } else if (AbStrUtil.isEmpty(mCetMiaoshu)) {
            ToastUtil.showMessage(this, "问题描述");
            return false;
        }
        return true;
    }

    @Override
    public void onMessageResponse(String url, JSONObject jo) throws JSONException {
        if (url.equals(Constant.ORDER_FEEDBACK_SUBMIT) && jo != null) {
            Login m_login = new Login(jo.toString());

            if (m_login != null) {


                if (m_login.getCode().equals("200")) {

                    ToastUtil.showMessage(this, "服务申请已提交 静候服务人员洽询");

                    setResult(200);
                    finish();
                } else {
                    ToastUtil.showMessage(this, m_login.getMsg());
                }
            }

        }
    }

    @Override
    public void onMessageResponse(String url, JSONArray jo) throws JSONException {

    }

    @Override
    public void onMessageResponse(String url, String str) throws Exception {

    }
}
